package Man.glass.bean;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import Man.glass.entity.user;
import Man.glass.mgr.AppManagerMgr;

@ManagedBean(name="appLRBean")
@SessionScoped
public class AppLRBean {
	//登陆
	private String inputName;
	private String inputPass;
	//注册
	private String reName;
	private String rePass;
	private String reDPass;
	private String rePhone;
	private String reEmail;
	//Mgr层接口
	private AppManagerMgr AMM = new AppManagerMgr();
	
	public void initPage(){
		reDPass="";
		reName="";
		rePass="";
		rePhone="";
		reEmail="";
		this.inputName="";
		this.inputPass="";
	}
	
	public AppLRBean(){
		reDPass="";
		reName="";
		rePass="";
		rePhone="";
		reEmail="";
		this.inputName="";
		this.inputPass="";
	}
	public String checkRegister(){
		if(reName!="" && reDPass!="" && rePass!="" && rePhone!="" && reEmail!=""){
			if(rePass.equals(reDPass)){
				List<user> userList =AMM.queryUser();
				user u =new user();
				u.setId(userList.size()+1);
				u.setName(reName);
				u.setPassword(rePass);
				u.setPhone(rePhone);
				u.setEmail(reEmail);
				boolean registerOK = AMM.addUser(u);
				if(registerOK){
					initPage();
					return "/page/login";
				}else{
					return null;
				}
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("The password is not same!")); 
				return null;
			}
		}else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("The input box can not be empty!")); 
			return null;
		}
	}
	public String checkLogin(){
		if(inputName!=""&&inputPass!=""){
			List<user> userList =AMM.queryUser();
			boolean loginOK=false;
			for(user u : userList){
				if(u.getName().equals(inputName) && u.getPassword().equals(inputPass)){
					loginOK=true;
					break;
				}
			}
			if(loginOK){
				initPage();
				return "/page/index";
			}else{
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("The user is not exist!")); 
				return null;
			}
		}else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("The input box can not be empty!")); 
			return null;
		}
	}
	
	public String goToRegister(){
		initPage();
		return "/page/register";
	}
	
	public String goToLogin(){
		initPage();
		return "/page/login";
	}
	
	public String getInputName() {
		return inputName;
	}
	public void setInputName(String inputName) {
		this.inputName = inputName;
	}
	public String getInputPass() {
		return inputPass;
	}
	public void setInputPass(String inputPass) {
		this.inputPass = inputPass;
	}

	public String getReName() {
		return reName;
	}

	public void setReName(String reName) {
		this.reName = reName;
	}

	public AppManagerMgr getAMM() {
		return AMM;
	}

	public void setAMM(AppManagerMgr aMM) {
		AMM = aMM;
	}

	public String getRePass() {
		return rePass;
	}

	public void setRePass(String rePass) {
		this.rePass = rePass;
	}

	public String getReDPass() {
		return reDPass;
	}

	public void setReDPass(String reDPass) {
		this.reDPass = reDPass;
	}

	public String getRePhone() {
		return rePhone;
	}

	public void setRePhone(String rePhone) {
		this.rePhone = rePhone;
	}

	public String getReEmail() {
		return reEmail;
	}

	public void setReEmail(String reEmail) {
		this.reEmail = reEmail;
	}
	
}
